﻿Ext.define('Oa.InfoMarketGrid', {
    extend: 'Ext.grid.Panel',
    alias: 'Widget.InfoMarketGrid',

    // override
    initComponent: function () {

        this.columns = [
        {
            header: "公司名称",dataIndex: 'CorpName',width:180
        }, {
            header: "信息类别", dataIndex: 'ClassName', width: 80
        }, {
            header: "标题",dataIndex: 'Title',flex:1
        }, {
            header: "信息来源", dataIndex: 'InfoFrom', width: 150
        }, {
            header: "提交人", dataIndex: 'Name', width: 80
        }, {
            header: "提交日期", dataIndex: 'InsertDate', width: 150
        }, {
            header: "状态", dataIndex: 'IsPass', width:60,
            renderer: function (value) { var retStr; if (value == 0) { retStr = "待审"; } else if (value == 1) { retStr = "通过"; } else if (value == 2) { retStr = "退件"; } return retStr; }
        }, { header: "操作", dataIndex: 'Id', width: 130, renderer: function (value, a, reader, c, d, ds) {
            var links = [];
            links.push('<a href="InfoMarketView.aspx?Id=' + value + '" target="_blank">查看</a>'); //编辑,删除和审核 存在并行操作Bug
            if ((reader.data.EmpId == Cookies.get("empId") || ",292,392,".indexOf(reader.data.EmpId) >= 0) && reader.data.IsPass != 1) {
                links.push('<a href="InfoMarketEdit.aspx?Id=' + value + '" target="_blank">编辑</a>');
                links.push('<a href="#" onclick="return formHandle(\'Del\',' + value + ');">删除</a>');
            }
            if (",292,392,".indexOf(reader.data.EmpId) >= 0 && reader.data.IsPass == 0) {
                links.push('<a href="#" onclick="return formHandle(\'Pass\',' + value + ');">通过</a>');
                links.push('<a href="#" onclick="return formHandle(\'Reject\',' + value + ');">退件</a>');
            }
            return links.join("&nbsp;");
        }
        }];
        // Set up a model to use in our Store
    Ext.define('InfoMarketModel', {
            extend: 'Ext.data.Model',
            fields: [{
                name: 'Id'
            }, {
                name: 'CorpName'
            }, {
                name: 'ClassName'
            }, {
                name: 'Title'
            }, {
                name: 'InfoFrom'
            }, {
                name: 'EmpId'
            }, {
                name: 'Name'
            }, {
                name: 'InsertDate'
            }, {
                name: 'IsPass'
            }]
        });

        var ds = Ext.create('Ext.data.Store', {
            model: 'InfoMarketModel',
            pageSize: 20, // items per page
            proxy: {
                type: 'ajax',
                url: '/InfoMarket.mvc/Find',
                reader: {
                    type: 'json',
                    root: 'data',
                    totalProperty: 'totalCount'//分页使用
                }
            },
            autoLoad: false
        });
        ds.load({ params: { start: 0, limit: 20} });
        ds.on('beforeload', function (thiz, options) {
            Ext.apply(thiz.proxy.extraParams, { corpName: Ext.getCmp('ddlCorpName').getValue(), className: Ext.getCmp('ddlClassName').getValue(), title: Ext.getCmp('txtTitle').getValue() });
        });
        this.store = ds;

        this.dockedItems = [{
            xtype: 'toolbar',
            dock: 'top',
            items: [{
                xtype: 'tbtext',
                text: '公司名称:'
            }, {
                xtype: 'combo',
                name: 'ddlCorpName',
                id: 'ddlCorpName',
                anchor: '100%',
                displayField: 'lb',
                valueField: 'lb',
                hiddenName: 'lb',
                hiddenId: 'lb',
                typeAhead: true,
                mode: 'local',
                editable: true,
                selectOnFoucs: true,
                triggerAction: 'all',
                blankText: '请选择或输入公司名称',
                emptyText: '请选择或输入公司名称',
                store: new Ext.data.SimpleStore({ fields: ['lb'],
                    data: [['政府监管部门'], ['直销媒体'], ['雅芳(中国)'], ['如新(中国)日用保健品'], ['宁波三生日用品'], ['宝健(中国)日用品']
                    , ['新时代健康产业(集团)'], ['富迪健康科技'], ['金士力佳友(天津)'], ['南京中脉科技发展'], ['安利(中国)日用品'], ['欧瑞莲化妆品(中国)'], ['广东康力医药']
                    , ['康宝莱(中国)保健品'], ['完美(中国)日用品'], ['无限极(中国)'], ['玫琳凯(中国)化妆品'], ['北京罗麦药业'], ['广东太阳神集团'], ['美乐家(中国)日用品']
                    , ['天津尚赫保健用品'], ['嘉康利(中国)日用品'], ['江苏安惠生物科技'], ['哈药集团股份'], ['克缇(中国)日用品'], ['江苏隆力奇生物科技股份']
                    , ['葆婴'], ['绿之韵生物工程集团'], ['天津天狮生物工程']], autoload: true
                }),
                listeners: { scope: this }
            }, {
                xtype: 'tbtext',
                text: '信息类别:'
            }, {
                xtype: 'combo',
                name: 'ddlClassName',
                id: 'ddlClassName',
                anchor: '100%',
                displayField: 'lb',
                valueField: 'lb',
                hiddenName: 'lb',
                hiddenId: 'lb',
                typeAhead: true,
                mode: 'local',
                editable: true,
                selectOnFoucs: true,
                triggerAction: 'all',
                blankText: '请选择或输入信息类别',
                emptyText: '请选择或输入信息类别',
                store: new Ext.data.SimpleStore({ fields: ['lb'],
                    data: [['公司简介'], ['营销策略'], ['公司高层'], ['管理团队'], ['主要系统'], ['市场精英'], ['新品上市'], ['促销活动'], ['专题活动']
                    , ['教育培训'], ['辅销资料'], ['重特大事件'], ['最新动态'], ['公司公告']], autoload: true
                }),
                listeners: { scope: this }
            }, {
                xtype: 'tbtext',
                text: '信息标题:'
            }, {
                xtype: 'textfield',
                name: 'txtTitle',
                id: 'txtTitle',
                emptyText: '请输入信息标题',
                blankText: '请输入信息标题'
            }, {
                text: "查询",
                iconCls: 'icon-search',
                handler: function () {
                    Ext.getCmp('pagingInfoMarket').moveFirst();
                }
            }, '-', {
                text: "添加",
                tooltip: "添加新的信息",
                iconCls: "addicon",
                handler: function () {
                    window.open('InfoMarketEdit.aspx');
                    return false;
                }
            }]
        }, {
            xtype: 'pagingtoolbar',
            id: 'pagingInfoMarket',
            store: ds,
            dock: 'bottom',
            displayInfo: true
        }];

        // finally call the superclasses implementation
        Oa.InfoMarketGrid.superclass.initComponent.call(this);
    }
});

//删除直销信息
function formHandle(operateCode, formId) {
    Ext.Ajax.request(
    {
        url: '/InfoMarket.mvc/FormHandle',
        success: function () {
            Ext.Msg.alert('提示', '已经成功保存修改的数据');
            if (Ext.getCmp("InfoMarketMgr") != null) { Ext.getCmp("InfoMarketMgr").getStore().reload(); }
        },
        failure: function (action) {
            if (action.failureType == 'server') {
                obj = Ext.JSON.decode(action.response.responseText);
                Ext.Msg.alert('错误:', obj.errors.reason);
            }
        },
        params: {
            operateCode: operateCode,
            formId: formId
        },
        waitMsg: '数据处理中...'
    });
}